﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CCL.Utility;

namespace CCL.Packet
{
    [Serializable]
    public sealed class PacketLogin : PacketObject
    {
        public char[] Email { get; private set; }
        public char[] Password { get; private set; }

        public PacketLogin(string email, string password)
            : base(Packet.Service.Login)
        {
            Email = new char[Packet.EmailLength];
            Password = new char[Packet.PasswordLength];

            StringUtility.StringCopy(Email, email, Packet.EmailLength);
            StringUtility.StringCopy(Password, password, Packet.PasswordLength);

            DataLength = Packet.EmailLength + Packet.PasswordLength;
        }

        public static new PacketLogin Parse(byte[] byteArray)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();

            stream.Write(byteArray, 0, byteArray.Length);
            stream.Position = 0;

            CCL.Packet.PacketLogin obj = formatter.Deserialize(stream) as CCL.Packet.PacketLogin;

            return obj;
        }
    }
}
